package com.lanhe.admin.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.lanhe.teacher.domain.vo.SelectTeacherVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.lanhe.common.annotation.Log;
import com.lanhe.common.core.controller.BaseController;
import com.lanhe.common.core.domain.AjaxResult;
import com.lanhe.common.core.page.TableDataInfo;
import com.lanhe.common.enums.BusinessType;
import com.lanhe.teacher.domain.LhTeacher;
import com.lanhe.teacher.service.ILhTeacherService;

/**
 * 老师表(LhTeacher)表控制层
 *
 * @author zhanggl
 * @since 2023-09-14 22:28:46
 */
@RestController
@RequestMapping("teacher/lhTeacher")
public class LhTeacherController extends BaseController {

    /**
     * 服务对象
     */
    @Resource
    private ILhTeacherService lhTeacherService;

    /**
     * 分页查询所有数据
     */
    @PreAuthorize("@ss.hasPermi('teacher:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhTeacher teacher) {
        startPage();
        List<LhTeacher> list = lhTeacherService.selectTeacherList(teacher);
        return getDataTable(list);
    }

    /**
     * 根据老师编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('teacher:teacher:query')")
    @GetMapping(value = { "/", "/{teacherId}" })
    public AjaxResult getInfo(@PathVariable(value = "teacherId") Long teacherId)
    {
        return success(lhTeacherService.selectTeacherById(teacherId));
    }

    /**
     * 新增老师
     */
    @PreAuthorize("@ss.hasPermi('teacher:teacher:add')")
    @Transactional
    @Log(title = "老师管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LhTeacher teacher)
    {
        if (null == teacher.getGroupId() || teacher.getGroupId()<=0)
        {
            return error("请选择所在的小组");
        }
        System.err.println("teacher "+teacher);
        if (!lhTeacherService.checkTeacherNameUnique(teacher))
        {
            return error("新增'" + teacher.getTeacherName() + "'失败，老师名称已存在");
        }
            teacher.setCreateBy(getUsername());
        lhTeacherService.insertTeacher(teacher);
        // lhGroupService.updateGroupTeacherNumInc(teacher.getGroupId());
        return success();
    }

    /**
     * 修改老师
     */
    @PreAuthorize("@ss.hasPermi('teacher:teacher:edit')")
    @Log(title = "老师管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LhTeacher teacher)
    {
        if (ObjectUtils.isEmpty(teacher.getTeacherId()) || null == teacher.getTeacherId())
        {
            return error("老师主键ID不能为空");
        }
        if (!lhTeacherService.checkTeacherNameUnique(teacher))
        {
            return error("修改'" + teacher.getTeacherName() + "'失败，老师名称已存在");
        }
        teacher.setUpdateBy(getUsername());
        System.err.println("teacher "+teacher);
        return toAjax(lhTeacherService.updateTeacher(teacher));
    }

    /**
     * 删除老师
     */
    @PreAuthorize("@ss.hasPermi('teacher:teacher:remove')")
    @Transactional
    @Log(title = "老师管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds)
    {
//        List<LhTeacher> lhTeachers = lhTeacherService.selectTeacherByIds(teacherIds);
//        for (LhTeacher lhTeacher : lhTeachers) {
//            lhGroupService.updateGroupTeacherNumDec(lhTeacher.getGroupId());
//        }
        lhTeacherService.deleteTeacherByIds(teacherIds);
        return success();
    }


    @GetMapping("/teacherList")
    public List<SelectTeacherVo> teacherList(LhTeacher lhteacher) {
        List<SelectTeacherVo> selectTeacherVos = new ArrayList<>();

        List<LhTeacher> list = lhTeacherService.selectTeacherList(lhteacher);
        list.forEach(teacher ->{
            SelectTeacherVo selectTeacherVo = new SelectTeacherVo();
            selectTeacherVo.setAddress(teacher.getTeacherId().toString());
            selectTeacherVo.setValue(teacher.getTeacherName());
            selectTeacherVos.add(selectTeacherVo);
        });
        return selectTeacherVos;
    }

}

